#!/usr/bin/env python

# -------------------------------
# projects/python/voting/main.py
# Copyright (C) 2009
# Glenn P. Downing
# -------------------------------

# To run the program
#     main.py < Voting.in > Voting.out

# To document the program
#     pydoc -w main

# -------
# globals
# -------

cases = 0 # input: don't change after reading
num_candidates = 0 # input: don't change after reading
winner = "" # output
candidates = [] #list of candidates

#
# Classes
#

class Ballot(object):
    """
    class that holds the information of a particular ballot and also
    who is the current winner of the ballot before reassigned if needed
    """
    def initiate(self, x):
        self.size = 0
        self.current_winner = 0
        self.vote = []
        numbers = x.split()
        i = 0
        while (i < len(numbers)):
            self.vote.append(numbers[i])
            i += 1
    # @return the current winner in the list of candidates in this ballot
    def get_Winner(self):
        return self.vote[self.current_winner]
    #set current winner to next candidate
    def next_Winner(self):
        self.current_winner += 1
    #@return current winner position
    def current_Win(self):
        return self.current_winner
    #prints votes in the ballot
    def print_Votes(self):
        i = 0
        while ( i < len(self.vote) ):
            OutputWriter().write( self.vote[i], " " )
            i += 1
        OutputWriter().write()

class Candidate(object):
    """
    the class Candidate for holding the informatino of each of the
    participants of the australian vote
    """
    def initiate(self, Cname, num):
        self.ballots = []
        self.votes = 0
        self.loser = False
        self.name = Cname
        self.number = num
    #@return candidates name
    def get_Name(self):
        return self.name
    #@return total amount of votes for this candidate
    def get_Votes(self):
        return self.votes
    #@return the position in the list of candidates this lies
    def get_Number(self):
        return self.number
    #@param assigns a ballot to the candidate
    def add_Ballot(self, ballot):
        self.ballots.append(ballot)
        self.votes += 1
    #@return the vector of ballots this candidate owns
    def get_Ballots(self):
        return self.ballots
    #shifts all this candidates current ballots to have a different winner
    def next_Winner(self):
        i = 0
        while (i < len(self.ballots) ):
            self.ballots[i].next_Winner()
            i += 1
    #labels this candidate a loser
    def lost(self):
        self.loser = True
    #@return whether the candidate isn't a loser
    def not_Lost(self):
        return (not self.loser)

# -----------
# InputReader
# -----------

class InputReader (object) :
    def read (self) :
        return raw_input()

# ------------
# OutputWriter
# ------------

class OutputWriter (object) :
    def write (self, *a) :
        for w in a :
            print w,
        print

# -------
# my_read
# -------

def my_read (r) :
    """
    reads an int into num_candidates and update the candidates
    if we get a EOF we know we are at the last case, the Ballots
    are also constructed and assigned to candidates
    return true if that succeeds, false otherwise
    """
    global num_candidates
    global candidates
    try :
        num_candidates = int(r.read())
        #gather candidates
        current_candidate = 0
        while (current_candidate < num_candidates):
            name = r.read()
            person = Candidate()
            person.initiate(name, current_candidate)
            candidates.append(person)
            current_candidate += 1
        vote = r.read()
        while (vote != ""):
            ballot = Ballot()
            ballot.initiate(vote)
            candidates[ int(ballot.get_Winner())-1 ].add_Ballot(ballot)
            vote = r.read()
        #give votes
    except EOFError :
        return False
    return True

# -------
# my_eval
# -------

def my_eval () :
    """
    Find out who the winner of the australian voting is, if a tie redistribute the votes
    to the other deserving  candidates until there is no longer a tie or that it is
    an official tie
    """
    global winner
    global candidates
    #if tie, dispurse votes, if tie, dispurse votes, rinse, repeat
    tie = True
    winner = ""
    while (tie):
        most_votes = 0
        total_votes=0
        least_votes = 1001
        winners = []
        losers = []
        i = 0
        while ( i<len(candidates) ):
            total_votes += candidates[i].get_Votes()
            if (candidates[i].get_Votes() > most_votes and candidates[i].not_Lost()):
                most_votes = candidates[i].get_Votes()
            if (candidates[i].get_Votes() < least_votes and candidates[i].not_Lost()):
                least_votes = candidates[i].get_Votes()
            i += 1
        i=0
        while ( i<len(candidates) ):
            if ( candidates[i].get_Votes() >= (total_votes / 2.0) and candidates[i].not_Lost()):
                winners.append( candidates[i] )
            if (candidates[i].get_Votes() == least_votes and least_votes != most_votes and candidates[i].not_Lost()):
                candidates[i].lost()
                losers.append( candidates[i] )
            i += 1
        if ( len(winners) == 1 or len(losers) == 0 ):            #if we have our winner(s) ''' or all_Tied()'''
            tie = False
            if ( len(winners) == 0):
                i = 0
                while ( i< len(candidates) ):
                    if (winner != ""):
                        if candidates[i].not_Lost():
                            winner += "\n"+candidates[i].get_Name()
                    else:
                        if candidates[i].not_Lost():
                            winner = candidates[i].get_Name()
                    i += 1
            else:
                i = 0
                while ( i< len(winners) ):
                    if (winner != ""):
                        winner += "\n"+winners[i].get_Name()
                    else:
                        winner = winners[i].get_Name()
                    i += 1
        else:                                                   #redistribute loser votes
            i = 0
            while  ( i< len(losers) ):
                candidates[ int(losers[i].get_Number()) ].next_Winner()
                ballots = candidates[ int(losers[i].get_Number()) ].get_Ballots()
                j = 0
                while ( j< len(ballots) ):
                    while(True):
                        if ( (not candidates[ int(ballots[j].get_Winner())-1 ].not_Lost() ) and  ballots[j].current_Win() < len(candidates) ):
                            ballots[j].next_Winner()
                        else:
                            break
                    candidates[ int(ballots[j].get_Winner())-1 ].add_Ballot( ballots[j] )
                    j += 1
                i += 1

# --------
# my_print
# --------

def my_print (w) :
    """
    writes the winner and a newline if needed
    """
    global cases
    w.write(winner)
    if (cases != 1):
        w.write("")

# ----
# main
# ----

def main () :
    """
    runs the program
    """
    global cases
    global candidates
    last = 0
    cases = int(InputReader().read())
    InputReader().read()
    while (cases > 0 and last != 1):
        candidates = []
        if (not my_read(InputReader())):
            last = 1
            #break
        my_eval()
        my_print(OutputWriter())
        cases -= 1

if __name__ == "__main__" :
    main()